package com.ruoyi.pos.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.pos.domain.cashier.PosCashier;
import com.ruoyi.pos.domain.dto.cashier.request.CashierLoginRequest;
import com.ruoyi.pos.domain.dto.cashier.request.PasswordChangeRequest;
import com.ruoyi.pos.domain.dto.cashier.response.CashierLoginResponse;
import com.ruoyi.pos.domain.dto.cashier.response.PasswordChangeResponse;
import com.ruoyi.pos.service.cashier.IPosCashierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * POS收银员Controller
 * 
 * @author ruoyi
 * @date 2025-01-30
 */
@Api(tags = "POS收银员管理")
@RestController
@RequestMapping("/pos/cashier")
public class PosCashierController extends BaseController {

    @Autowired
    private IPosCashierService posCashierService;

    /**
     * 收银员登录
     */
    @ApiOperation("收银员登录")
    @PostMapping("/login")
    public AjaxResult login(@RequestBody CashierLoginRequest request) {
        try {
            CashierLoginResponse response = posCashierService.cashierLogin(request);
            return success(response);
        } catch (Exception e) {
            return error("登录失败：" + e.getMessage());
        }
    }

    /**
     * 收银员登出
     */
    @ApiOperation("收银员登出")
    @PostMapping("/logout/{cashierId}/{deviceId}")
    public AjaxResult logout(@PathVariable Long cashierId, @PathVariable Long deviceId) {
        try {
            boolean result = posCashierService.cashierLogout(cashierId, deviceId);
            return result ? success("登出成功") : error("登出失败");
        } catch (Exception e) {
            return error("登出失败：" + e.getMessage());
        }
    }

    /**
     * 切换收银员
     */
    @ApiOperation("切换收银员")
    @PostMapping("/switch/{oldCashierId}/{newCashierId}/{deviceId}")
    public AjaxResult switchCashier(@PathVariable Long oldCashierId, 
                                   @PathVariable Long newCashierId, 
                                   @PathVariable Long deviceId) {
        try {
            boolean result = posCashierService.switchCashier(oldCashierId, newCashierId, deviceId);
            return result ? success("切换成功") : error("切换失败");
        } catch (Exception e) {
            return error("切换失败：" + e.getMessage());
        }
    }

    /**
     * 获取当前收银员信息
     */
    @ApiOperation("获取当前收银员信息")
    @GetMapping("/current/{deviceId}")
    public AjaxResult getCurrentCashier(@PathVariable Long deviceId) {
        try {
            CashierLoginResponse response = posCashierService.getCurrentCashier(deviceId);
            return success(response);
        } catch (Exception e) {
            return error("获取收银员信息失败：" + e.getMessage());
        }
    }

    /**
     * 验证收银员设备权限
     */
    @ApiOperation("验证收银员设备权限")
    @GetMapping("/check/device/{cashierId}/{deviceId}")
    public AjaxResult checkDevicePermission(@PathVariable Long cashierId, @PathVariable Long deviceId) {
        try {
            boolean result = posCashierService.checkCashierDevicePermission(cashierId, deviceId);
            return success(result);
        } catch (Exception e) {
            return error("权限验证失败：" + e.getMessage());
        }
    }

    /**
     * 验证收银员柜组权限
     */
    @ApiOperation("验证收银员柜组权限")
    @GetMapping("/check/group/{cashierId}/{groupId}")
    public AjaxResult checkGroupPermission(@PathVariable Long cashierId, @PathVariable Long groupId) {
        try {
            boolean result = posCashierService.checkCashierGroupPermission(cashierId, groupId);
            return success(result);
        } catch (Exception e) {
            return error("权限验证失败：" + e.getMessage());
        }
    }

    /**
     * 修改收银员密码
     */
    @ApiOperation("修改收银员密码")
    @Log(title = "收银员密码修改", businessType = BusinessType.UPDATE)
    @PostMapping("/changePassword")
    public AjaxResult changePassword(@RequestBody PasswordChangeRequest request) {
        try {
            PasswordChangeResponse response = posCashierService.changePassword(request);
            if (response.getSuccess()) {
                return success("密码修改成功");
            } else {
                return error(response.getMessage());
            }
        } catch (Exception e) {
            return error("密码修改失败：" + e.getMessage());
        }
    }

    /**
     * 查询POS收银员列表
     */
    @ApiOperation("查询收银员列表")
    @PreAuthorize("@ss.hasPermi('pos:cashier:list')")
    @GetMapping("/list")
    public TableDataInfo list(PosCashier posCashier) {
        startPage();
        List<PosCashier> list = posCashierService.selectPosCashierList(posCashier);
        return getDataTable(list);
    }

    /**
     * 导出POS收银员列表
     */
    @ApiOperation("导出收银员列表")
    @PreAuthorize("@ss.hasPermi('pos:cashier:export')")
    @Log(title = "POS收银员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PosCashier posCashier) {
        List<PosCashier> list = posCashierService.selectPosCashierList(posCashier);
        ExcelUtil<PosCashier> util = new ExcelUtil<PosCashier>(PosCashier.class);
        util.exportExcel(response, list, "POS收银员数据");
    }

    /**
     * 获取POS收银员详细信息
     */
    @ApiOperation("获取收银员详细信息")
    @PreAuthorize("@ss.hasPermi('pos:cashier:query')")
    @GetMapping(value = "/{cashierId}")
    public AjaxResult getInfo(@PathVariable("cashierId") Long cashierId) {
        return success(posCashierService.selectPosCashierByCashierId(cashierId));
    }

    /**
     * 新增POS收银员
     */
    @ApiOperation("新增收银员")
    @PreAuthorize("@ss.hasPermi('pos:cashier:add')")
    @Log(title = "POS收银员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PosCashier posCashier) {
        return toAjax(posCashierService.insertPosCashier(posCashier));
    }

    /**
     * 修改POS收银员
     */
    @ApiOperation("修改收银员")
    @PreAuthorize("@ss.hasPermi('pos:cashier:edit')")
    @Log(title = "POS收银员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PosCashier posCashier) {
        return toAjax(posCashierService.updatePosCashier(posCashier));
    }

    /**
     * 删除POS收银员
     */
    @ApiOperation("删除收银员")
    @PreAuthorize("@ss.hasPermi('pos:cashier:remove')")
    @Log(title = "POS收银员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{cashierIds}")
    public AjaxResult remove(@PathVariable Long[] cashierIds) {
        return toAjax(posCashierService.deletePosCashierByCashierIds(cashierIds));
    }
}
